package com.itheima.spring.boot.autoconfigure;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;


public class SMSClient {
    private SMSConfigProperties smsConfigProperties;
    private IAcsClient client;


    public SMSClient(SMSConfigProperties smsConfigProperties) {
        this.smsConfigProperties = smsConfigProperties;
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", smsConfigProperties.getAccessKeyId(), smsConfigProperties.getAccessSecret());
        client = new DefaultAcsClient(profile);
    }

    public CommonResponse sendCode(String phoneNumber, String msgCode) {

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", smsConfigProperties.getSignName());
        request.putQueryParameter("TemplateCode", smsConfigProperties.getTemplateCode());
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + msgCode + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
